<!--
 * @Descripttion:
 * @Author: Silence
 * @Date: 2023-06-06 21:35:35
 * @LastEditors: Silence
 * @LastEditTime: 2023-06-07 14:59:38
-->
<template>
  <div class="box">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="0px"
      class="demo-ruleForm"
    >
      <div class="img"><img src="../../assets/logo.png" alt="" /></div>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
        <div class="btns">
          <el-button size="small" type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button size="small" type="info" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (!/^\d{6,}$/.test(value)) {
        callback(new Error('请输入6位以上长度的密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { trigger: 'blur', requery: true, message: '请输入用户名' }
        ],
        password: [
          { requery: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur', requery: true }
        ]
      }
    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await this.$store.dispatch('user/syncLogin', this.ruleForm)
          this.$router.push('/')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang='scss'  scoped>
.box {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
  .el-form {
    width: 450px;
    height: 300px;
    background-color: #fff;
    padding: 100px 20px 0;
    .btns {
      position: absolute;
      right: 0;
      padding-top: 10px;
    }
    .img {
      position: absolute;
      top: 20px;
      left: 50%;
      transform: translate(-50%);
      z-index: 999;
      img {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background-color: #eee;
        border: solid 5px #fff;
      }
    }
  }
}
</style>
